package com.onlinelearn.courseservice.service;

import com.onlinelearn.courseservice.repository.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

@Service
public class CourseManagementService {
    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private NotificationTriggerService notificationService;

    public Course createCourse(Long teacherId, CourseDTO courseDTO) {
        // 验证教师权限
        validateTeacherPermission(teacherId);

        Course course = new Course();
        course.setTeacherId(teacherId);
        course.setTitle(courseDTO.getTitle());
        course.setDescription(courseDTO.getDescription());
        course.setCategory(courseDTO.getCategory());
        course.setPrice(courseDTO.getPrice());
        course.setStartTime(courseDTO.getStartTime());
        course.setStatus(CourseStatus.DRAFT);

        return courseRepository.save(course);
    }

    public Course uploadCourseResource(Long courseId, MultipartFile file, ResourceType type) {
        Course course = courseRepository.findById(courseId)
            .orElseThrow(() -> new CourseNotFoundException(courseId));

        // 文件格式和大小验证
        validateFileUpload(file, type);

        String fileUrl = fileStorageService.storeFile(file, type);

        switch (type) {
            case VIDEO:
                course.addVideo(new Video(fileUrl));
                break;
            case DOCUMENT:
                course.addDocument(new Document(fileUrl));
                break;
            case HOMEWORK_TEMPLATE:
                course.setHomeworkTemplate(fileUrl);
                break;
        }

        return courseRepository.save(course);
    }

    public Course publishCourse(Long courseId) {
        Course course = courseRepository.findById(courseId)
            .orElseThrow(() -> new CourseNotFoundException(courseId));

        // 发布前验证课程完整性
        validateCourseForPublish(course);

        course.setStatus(CourseStatus.PUBLISHED);
        Course publishedCourse = courseRepository.save(course);

        // 通知所有关注该教师的学生
        notificationService.triggerCourseUpdateNotification(
            courseId, 
            "新课程发布：" + course.getTitle()
        );

        return publishedCourse;
    }

    private void validateFileUpload(MultipartFile file, ResourceType type) {
        long MAX_FILE_SIZE = 2 * 1024 * 1024 * 1024L; // 2GB
        String[] ALLOWED_VIDEO_FORMATS = {"mp4", "webm"};
        String[] ALLOWED_DOCUMENT_FORMATS = {"pdf", "ppt", "pptx"};

        if (file.getSize() > MAX_FILE_SIZE) {
            throw new FileSizeExceededException("文件大小超过2GB限制");
        }

        String fileExtension = getFileExtension(file.getOriginalFilename());
        switch (type) {
            case VIDEO:
                if (!Arrays.asList(ALLOWED_VIDEO_FORMATS).contains(fileExtension)) {
                    throw new InvalidFileTypeException("仅支持MP4和WebM视频格式");
                }
                break;
            case DOCUMENT:
                if (!Arrays.asList(ALLOWED_DOCUMENT_FORMATS).contains(fileExtension)) {
                    throw new InvalidFileTypeException("仅支持PDF和PPT文档");
                }
                break;
        }
    }

    enum ResourceType {
        VIDEO,
        DOCUMENT,
        HOMEWORK_TEMPLATE
    }
} 